<template>
  <div class="app-container">
    <el-card style="border-radius: 10px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"  label-width="68px">
      <el-form-item label="转换单号" prop="materialConversionsCode">
        <el-input
          v-model="queryParams.materialConversionsCode"
          placeholder="请输入转换单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="出库仓库ID" prop="outWarehouseId">
        <el-input
          v-model="queryParams.outWarehouseId"
          placeholder="请输入出库仓库ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入库仓库ID" prop="intWarehouseId">
        <el-input
          v-model="queryParams.intWarehouseId"
          placeholder="请输入入库仓库ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="单据状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择单据状态" clearable>
          <el-option
            v-for="dict in dict.type.material_conversion_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-card>
    <div style="height: 15px"></div>
    <el-card style="border-radius: 10px">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleUpdate()"
          v-hasPermi="['wm:materialconversions:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wm:materialconversions:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wm:materialconversions:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="materialconversionsList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="转换单号" align="center" prop="materialConversionsCode" >
        <template slot-scope="scope">
          <el-button
              type="text"
              @click="handleView(scope.row.id)"
          >{{scope.row.materialConversionsCode}}</el-button>
        </template>
      </el-table-column>

      <el-table-column label="出库仓库" align="center" prop="outWarehouseName" />
      <el-table-column label="入库仓库" align="center" prop="intWarehouseName" />
      <el-table-column label="单据状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.material_conversion_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="单据日期" align="center" prop="billDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.billDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="230px" fixed="right" align="center"  >
        <template slot-scope="scope">
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row.id)"
            v-hasPermi="['wm:materialconversions:edit']"
          >修改</el-button>-->
            <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row.id)"
                v-if="scope.row.status == 0"
                v-hasPermi="['wm:materialconversions:edit']"
            >修改</el-button>

            <el-button
                size="mini"
                type="text"
                icon="el-icon-s-check"
                @click="tijiao(scope.row.id)"
                v-if="scope.row.status == 0"
                v-hasPermi="['wm:materialconversions:edit']"
            >提交</el-button>

            <el-button
                size="mini"
                type="text"
                icon="el-icon-video-play"
                @click="shenhe(scope.row.id)"
                v-if="scope.row.status == 1"
                v-hasPermi="['wm:materialconversions:edit']"
            >执行入库</el-button>

            <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-if="scope.row.status == 0"
                v-hasPermi="['wm:materialconversions:remove']"
            >删除</el-button>

              <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-view"
                  @click="handleView(scope.row.id)"
                  v-if="scope.row.status == 3"
                  v-hasPermi="['wm:materialconversions:remove']"
              >查看</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>
    <!-- 添加或修改形态转换单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="转换单号8888" prop="materialConversionsCode">
              <el-input v-model="form.materialConversionsCode" placeholder="请输入转换单号" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label-width="80">
              <el-switch v-model="autoGenFlag"
                         active-color="#13ce66"
                         active-text="自动生成"
                         @change="handleAutoGenChange(autoGenFlag)" v-if="optType !== 'view'">
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单据日期" prop="billDate">
              <el-date-picker clearable style="width: 100%"
                              v-model="form.billDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择单据日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
       <el-row>
         <el-col :span="12">
           <el-form-item label="出库仓库" prop="outWarehouseId">
<!--             <el-input v-model="form.outWarehouseId" placeholder="请输入出库仓库ID" />-->
             <el-select v-model="form.outWarehouseId" placeholder="请选择出库仓库" @change="handleOutWarehouseChange" style="width: 100%">
               <el-option
                   v-for="warehouse in warehouseList"
                   :key="warehouse.warehouseId"
                   :label="warehouse.warehouseName"
                   :value="warehouse.warehouseId">
               </el-option>
             </el-select>
           </el-form-item>
         </el-col>
         <el-col :span="12">
           <el-form-item label="入库仓库" prop="intWarehouseId">
<!--             <el-input v-model="form.intWarehouseId" placeholder="请输入入库仓库ID" />-->
             <el-select v-model="form.intWarehouseId" placeholder="请选择出库仓库" @change="handleInWarehouseChange" style="width: 100%">
               <el-option
                   v-for="warehouse in warehouseList"
                   :key="warehouse.warehouseId"
                   :label="warehouse.warehouseName"
                   :value="warehouse.warehouseId">
               </el-option>
             </el-select>
           </el-form-item>
         </el-col>
       </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>

    </el-dialog>
    <conversions-line ref="zhlist" v-if="devisible" @refreshDataList="getList"></conversions-line>
    <conversion-view ref="conView" v-if="detailVisible" @refreshDataList="getList"></conversion-view>
  </div>
</template>

<script>
import {
  listMaterialconversions,
  getMaterialconversions,
  delMaterialconversions,
  addMaterialconversions,
  updateMaterialconversions,
  shenhe, tijiao
} from "@/api/mes/wm/materialconversions";
import {genCode} from "@/api/system/autocode/rule";
import {getWarehouseList} from "@/api/mes/wm/warehouse";
import  ConversionsLine from "./conversionsline.vue"
import ConversionView from "./conversionview.vue"
export default {
  name: "Materialconversions",
  dicts: ['material_conversion_status'],
  components:{ConversionsLine,ConversionView},
  data() {
    return {
      detailVisible:false,
      autoGenFlag:false,
      optType: undefined,
      warehouseList:[],
      devisible:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 形态转换单表格数据
      materialconversionsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialConversionsCode: null,        outWarehouseId: null,        outWarehouseCode: null,        outWarehouseName: null,        intWarehouseId: null,        intWarehouseCode: null,        intWarehouseName: null,        status: null,      },
      // 表单参数
      form: {

      },
      // 表单校验
      rules: {
        outWarehouseId: [
          { required: true, message: "出库仓库ID不能为空", trigger: "blur" }
        ],        intWarehouseId: [
          { required: true, message: "入库仓库ID不能为空", trigger: "blur" }
        ],      }
    };
  },
  created() {
    this.getList();
    this.getWarehouse();
  },
  methods: {
    /** 查询形态转换单列表 */
    getList() {
      this.loading = true;
      listMaterialconversions(this.queryParams).then(response => {
        this.materialconversionsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //获取仓库
    getWarehouse(){
      this.warehouseList=[]
      getWarehouseList().then(response => {
        this.warehouseList = response.data.filter((el) =>{
          return el.warehouseCode.indexOf('VIR') == -1;
        })
      });
    },
    //选择chu仓库
    handleOutWarehouseChange(value){
      this.form.outWarehouseCode=''
      this.form.outWarehouseName=''
      this.form.outWarehouseName=this.warehouseList.filter(item => item.warehouseId === value)[0].warehouseName
    },
    //选择入仓库
    handleInWarehouseChange(value){
      this.form.intWarehouseCode=''
      this.form.intWarehouseName=''
      this.form.intWarehouseName=this.warehouseList.filter(item => item.warehouseId === value)[0].warehouseName

    },
    //自动生成编码
    handleAutoGenChange(autoGenFlag) {
      if (autoGenFlag) {
        genCode('MATERIAL_CONVERSIONS_CODE').then(response => {
          this.form.materialConversionsCode = response;
        });
      } else {
        this.form.materialConversionsCode = null;
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,        materialConversionsCode: null,        outWarehouseId: null,        outWarehouseCode: null,        outWarehouseName: null,        intWarehouseId: null,        intWarehouseCode: null,        intWarehouseName: null,        status: null,        billDate: null,        createBy: null,        createByName: null,        createTime: null,        updateBy: null,        updateByName: null,        updateTime: null,        tenantId: null      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.optType = "add";
      this.title = "添加形态转换单";
    },
    //审核入库
    shenhe(id){
      this.$modal.confirm('审核入库?').then(function() {
        return shenhe(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {

      });
    },
    //提交
    tijiao(id){

      tijiao(id).then(response => {
        if(response.code == 200){
          this.$modal.msgSuccess("提交成功");
          this.getList();
        }else {
          this.$modal.msgSuccess("提交失败");
        }

      });
    },
    //查看
    handleView(id){
      this.detailVisible=true
      this.$nextTick(() => {
        this.$refs.conView.init(id)
      })
    },
    /** 修改按钮操作 */
    handleUpdate(id) {
 /*     this.reset();
      const id = row.id || this.ids
      getMaterialconversions(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.optType="edit"
        this.title = "修改形态转换单";
      });*/
      this.devisible=true
      // this.title="修改形态转换单"
      this.$nextTick(() => {
        this.$refs.zhlist.init(id)
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMaterialconversions(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMaterialconversions(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除形态转换单?').then(function() {
        return delMaterialconversions(ids);
      }).then(() => {

        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wm/materialconversions/export', {
        ...this.queryParams
      }, `materialconversions_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
